﻿/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：曾永佑
 * 版本：v1.0
 * 创建日期：2013-11-1
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SRF.Common.Logging
{
    /// <summary>
    /// 跟踪器
    /// </summary>
    public class Tracer
    {
        Stopwatch watch;
        string name;

        static string _logFormat;
        /// <summary>
        /// log format
        /// </summary>
        public static string LogFormat
        {
            get
            {
                if (string.IsNullOrEmpty(_logFormat))
                    _logFormat = "跟踪[{0}] 耗时:{1} ms";

                return _logFormat;
            }
            set
            {
                _logFormat = value;
            }
        }

        /// <summary>
        /// 开始
        /// </summary>
        /// <returns></returns>
        public static Tracer StartNew(string name = "")
        {
            var tracer = new Tracer();
            tracer.Start(name);

            return tracer;
        }

        public void Start(string name = "")
        {
            this.name = name;
            watch = Stopwatch.StartNew();
        }
        public void Stop()
        {
            if (watch != null && watch.IsRunning)
            {
                watch.Stop();

                LogManager.GetCurrentLogger().DebugFormat(LogFormat, this.name, watch.ElapsedMilliseconds);
            }
        }
    }
}